// src/main/java/com/xxxy/ljh/myapplication/DaoImpl/ActivityDaoImpl.java
package com.xxxy.ljh.myapplication.DaoImpl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xxxy.ljh.myapplication.Dao.ActivityDao;
import com.xxxy.ljh.myapplication.dphelper.sqliteDBHelper;
import com.xxxy.ljh.myapplication.model.ActivityItem;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动数据访问接口实现类，用于操作数据库中的活动信息。
 */
public class ActivityDaoImpl implements ActivityDao {

    private Context mContext;
    private SQLiteDatabase mDatabase;

    /**
     * 构造方法，初始化数据库连接。
     *
     * @param context 应用上下文对象
     */
    public ActivityDaoImpl(Context context) {
        mContext = context;
        mDatabase = new sqliteDBHelper(mContext).getWritableDatabase();
    }

    /**
     * 创建一个新的活动记录。
     *
     * @param activity 要插入的活动对象
     * @return 插入成功返回行ID，失败返回-1
     */
    @Override
    public long createActivity(ActivityItem activity) {
        ContentValues values = new ContentValues();
        values.put("activity_name", activity.getTitle());
        values.put("association_id", activity.getAssociationId());
        values.put("organizer_id", activity.getOrganizerId());
        values.put("description", activity.getDescription());
        values.put("activity_type", activity.getActivityType());
        values.put("start_time", activity.getStartTime());
        values.put("end_time", activity.getEndTime());
        values.put("location", activity.getLocation());
        values.put("max_participants", activity.getMaxParticipants());
        values.put("signup_deadline", activity.getSignupDeadline());
        values.put("signup_requirement", activity.getSignupRequirement());
        values.put("points_rule", activity.getPointsRule());
        values.put("cost", activity.getCost());
        values.put("cost_description", activity.getCostDescription());
        values.put("status", activity.getStatus());
        values.put("cover_image", activity.getCoverImage());

        long result = mDatabase.insert("activity", null, values);
        return result;
    }

    /**
     * 根据活动ID查询单个活动详情。
     *
     * @param activityId 活动唯一标识符
     * @return 查询到的活动对象，未找到则返回null
     */
    @Override
    public ActivityItem getActivityById(int activityId) {
        String selection = "activity_id = ?";
        String[] selectionArgs = {String.valueOf(activityId)};
        Cursor cursor = mDatabase.query("activity", null, selection, selectionArgs, null, null, null);

        ActivityItem activity = null;
        if (cursor.moveToFirst()) {
            activity = new ActivityItem();
            activity.setId(cursor.getInt(cursor.getColumnIndexOrThrow("activity_id")));
            activity.setTitle(cursor.getString(cursor.getColumnIndexOrThrow("activity_name")));
            activity.setAssociationId(cursor.getInt(cursor.getColumnIndexOrThrow("association_id")));
            activity.setOrganizerId(cursor.getInt(cursor.getColumnIndexOrThrow("organizer_id")));
            activity.setDescription(cursor.getString(cursor.getColumnIndexOrThrow("description")));
            activity.setActivityType(cursor.getString(cursor.getColumnIndexOrThrow("activity_type")));
            activity.setStartTime(cursor.getString(cursor.getColumnIndexOrThrow("start_time")));
            activity.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow("end_time")));
            activity.setLocation(cursor.getString(cursor.getColumnIndexOrThrow("location")));
            activity.setMaxParticipants(cursor.getInt(cursor.getColumnIndexOrThrow("max_participants")));
            activity.setSignupDeadline(cursor.getString(cursor.getColumnIndexOrThrow("signup_deadline")));
            activity.setSignupRequirement(cursor.getString(cursor.getColumnIndexOrThrow("signup_requirement")));
            activity.setPointsRule(cursor.getInt(cursor.getColumnIndexOrThrow("points_rule")));
            activity.setCost(cursor.getDouble(cursor.getColumnIndexOrThrow("cost")));
            activity.setCostDescription(cursor.getString(cursor.getColumnIndexOrThrow("cost_description")));
            activity.setStatus(cursor.getInt(cursor.getColumnIndexOrThrow("status")));
            activity.setCoverImage(cursor.getString(cursor.getColumnIndexOrThrow("cover_image")));
            activity.setCreateTime(cursor.getString(cursor.getColumnIndexOrThrow("create_time")));
            activity.setUpdateTime(cursor.getString(cursor.getColumnIndexOrThrow("update_time")));
            activity.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("remark")));
        }
        cursor.close();
        return activity;
    }

    /**
     * 获取所有活动列表，并按创建时间倒序排列。
     *
     * @return 所有活动组成的列表
     */
    @Override
    public List<ActivityItem> getAllActivities() {
        Cursor cursor = mDatabase.query("activity", null, null, null, null, null, "create_time DESC");

        List<ActivityItem> activities = new ArrayList<>();
        while (cursor.moveToNext()) {
            ActivityItem activity = new ActivityItem();
            activity.setId(cursor.getInt(cursor.getColumnIndexOrThrow("activity_id")));
            activity.setTitle(cursor.getString(cursor.getColumnIndexOrThrow("activity_name")));
            activity.setAssociationId(cursor.getInt(cursor.getColumnIndexOrThrow("association_id")));
            activity.setOrganizerId(cursor.getInt(cursor.getColumnIndexOrThrow("organizer_id")));
            activity.setDescription(cursor.getString(cursor.getColumnIndexOrThrow("description")));
            activity.setActivityType(cursor.getString(cursor.getColumnIndexOrThrow("activity_type")));
            activity.setStartTime(cursor.getString(cursor.getColumnIndexOrThrow("start_time")));
            activity.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow("end_time")));
            activity.setLocation(cursor.getString(cursor.getColumnIndexOrThrow("location")));
            activity.setMaxParticipants(cursor.getInt(cursor.getColumnIndexOrThrow("max_participants")));
            activity.setSignupDeadline(cursor.getString(cursor.getColumnIndexOrThrow("signup_deadline")));
            activity.setSignupRequirement(cursor.getString(cursor.getColumnIndexOrThrow("signup_requirement")));
            activity.setPointsRule(cursor.getInt(cursor.getColumnIndexOrThrow("points_rule")));
            activity.setCost(cursor.getDouble(cursor.getColumnIndexOrThrow("cost")));
            activity.setCostDescription(cursor.getString(cursor.getColumnIndexOrThrow("cost_description")));
            activity.setStatus(cursor.getInt(cursor.getColumnIndexOrThrow("status")));
            activity.setCoverImage(cursor.getString(cursor.getColumnIndexOrThrow("cover_image")));
            activity.setCreateTime(cursor.getString(cursor.getColumnIndexOrThrow("create_time")));
            activity.setUpdateTime(cursor.getString(cursor.getColumnIndexOrThrow("update_time")));
            activity.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("remark")));
            activities.add(activity);
        }
        cursor.close();
        return activities;
    }

    /**
     * 根据社团ID获取该社团发布的所有活动，并按创建时间倒序排列。
     *
     * @param associationId 社团唯一标识符
     * @return 属于指定社团的所有活动组成的列表
     */
    @Override
    public List<ActivityItem> getActivitiesByAssociationId(int associationId) {
        String selection = "association_id = ?";
        String[] selectionArgs = {String.valueOf(associationId)};
        Cursor cursor = mDatabase.query("activity", null, selection, selectionArgs, null, null, "create_time DESC");

        List<ActivityItem> activities = new ArrayList<>();
        while (cursor.moveToNext()) {
            ActivityItem activity = new ActivityItem();
            activity.setId(cursor.getInt(cursor.getColumnIndexOrThrow("activity_id")));
            activity.setTitle(cursor.getString(cursor.getColumnIndexOrThrow("activity_name")));
            activity.setAssociationId(cursor.getInt(cursor.getColumnIndexOrThrow("association_id")));
            activity.setOrganizerId(cursor.getInt(cursor.getColumnIndexOrThrow("organizer_id")));
            activity.setDescription(cursor.getString(cursor.getColumnIndexOrThrow("description")));
            activity.setActivityType(cursor.getString(cursor.getColumnIndexOrThrow("activity_type")));
            activity.setStartTime(cursor.getString(cursor.getColumnIndexOrThrow("start_time")));
            activity.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow("end_time")));
            activity.setLocation(cursor.getString(cursor.getColumnIndexOrThrow("location")));
            activity.setMaxParticipants(cursor.getInt(cursor.getColumnIndexOrThrow("max_participants")));
            activity.setSignupDeadline(cursor.getString(cursor.getColumnIndexOrThrow("signup_deadline")));
            activity.setSignupRequirement(cursor.getString(cursor.getColumnIndexOrThrow("signup_requirement")));
            activity.setPointsRule(cursor.getInt(cursor.getColumnIndexOrThrow("points_rule")));
            activity.setCost(cursor.getDouble(cursor.getColumnIndexOrThrow("cost")));
            activity.setCostDescription(cursor.getString(cursor.getColumnIndexOrThrow("cost_description")));
            activity.setStatus(cursor.getInt(cursor.getColumnIndexOrThrow("status")));
            activity.setCoverImage(cursor.getString(cursor.getColumnIndexOrThrow("cover_image")));
            activity.setCreateTime(cursor.getString(cursor.getColumnIndexOrThrow("create_time")));
            activity.setUpdateTime(cursor.getString(cursor.getColumnIndexOrThrow("update_time")));
            activity.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("remark")));
            activities.add(activity);
        }
        cursor.close();
        return activities;
    }

    /**
     * 更新一个已存在的活动信息。
     *
     * @param activity 包含更新后信息的活动对象
     * @return 更新是否成功
     */
    @Override
    public boolean updateActivity(ActivityItem activity) {
        ContentValues values = new ContentValues();
        values.put("activity_name", activity.getTitle());
        values.put("association_id", activity.getAssociationId());
        values.put("organizer_id", activity.getOrganizerId());
        values.put("description", activity.getDescription());
        values.put("activity_type", activity.getActivityType());
        values.put("start_time", activity.getStartTime());
        values.put("end_time", activity.getEndTime());
        values.put("location", activity.getLocation());
        values.put("max_participants", activity.getMaxParticipants());
        values.put("signup_deadline", activity.getSignupDeadline());
        values.put("signup_requirement", activity.getSignupRequirement());
        values.put("points_rule", activity.getPointsRule());
        values.put("cost", activity.getCost());
        values.put("cost_description", activity.getCostDescription());
        values.put("status", activity.getStatus());
        values.put("cover_image", activity.getCoverImage());
        values.put("update_time", activity.getUpdateTime());
        values.put("remark", activity.getRemark());

        String whereClause = "activity_id = ?";
        String[] whereArgs = {String.valueOf(activity.getId())};

        int result = mDatabase.update("activity", values, whereClause, whereArgs);
        return result > 0;
    }

    /**
     * 删除指定ID的活动记录。
     *
     * @param activityId 要删除的活动唯一标识符
     * @return 删除是否成功
     */
    @Override
    public boolean deleteActivity(int activityId) {
        String whereClause = "activity_id = ?";
        String[] whereArgs = {String.valueOf(activityId)};

        int result = mDatabase.delete("activity", whereClause, whereArgs);
        return result > 0;
    }

    /**
     * 获取即将开始且状态正常的活动（即开始时间大于当前时间并且状态为1），
     * 并按照开始时间升序排序。
     *
     * @return 符合条件的活动列表
     */
    @Override
    public List<ActivityItem> getUpcomingActivities() {
        // 查询条件：活动尚未开始且处于正常发布状态(status=1)
        String selection = "start_time > datetime('now') AND status = ?";
        String[] selectionArgs = {"1"};
        Cursor cursor = mDatabase.query("activity", null, selection, selectionArgs, null, null, "start_time ASC");

        List<ActivityItem> activities = new ArrayList<>();
        while (cursor.moveToNext()) {
            ActivityItem activity = new ActivityItem();
            activity.setId(cursor.getInt(cursor.getColumnIndexOrThrow("activity_id")));
            activity.setTitle(cursor.getString(cursor.getColumnIndexOrThrow("activity_name")));
            activity.setAssociationId(cursor.getInt(cursor.getColumnIndexOrThrow("association_id")));
            activity.setOrganizerId(cursor.getInt(cursor.getColumnIndexOrThrow("organizer_id")));
            activity.setDescription(cursor.getString(cursor.getColumnIndexOrThrow("description")));
            activity.setActivityType(cursor.getString(cursor.getColumnIndexOrThrow("activity_type")));
            activity.setStartTime(cursor.getString(cursor.getColumnIndexOrThrow("start_time")));
            activity.setEndTime(cursor.getString(cursor.getColumnIndexOrThrow("end_time")));
            activity.setLocation(cursor.getString(cursor.getColumnIndexOrThrow("location")));
            activity.setMaxParticipants(cursor.getInt(cursor.getColumnIndexOrThrow("max_participants")));
            activity.setSignupDeadline(cursor.getString(cursor.getColumnIndexOrThrow("signup_deadline")));
            activity.setSignupRequirement(cursor.getString(cursor.getColumnIndexOrThrow("signup_requirement")));
            activity.setPointsRule(cursor.getInt(cursor.getColumnIndexOrThrow("points_rule")));
            activity.setCost(cursor.getDouble(cursor.getColumnIndexOrThrow("cost")));
            activity.setCostDescription(cursor.getString(cursor.getColumnIndexOrThrow("cost_description")));
            activity.setStatus(cursor.getInt(cursor.getColumnIndexOrThrow("status")));
            activity.setCoverImage(cursor.getString(cursor.getColumnIndexOrThrow("cover_image")));
            activity.setCreateTime(cursor.getString(cursor.getColumnIndexOrThrow("create_time")));
            activity.setUpdateTime(cursor.getString(cursor.getColumnIndexOrThrow("update_time")));
            activity.setRemark(cursor.getString(cursor.getColumnIndexOrThrow("remark")));
            activities.add(activity);
        }
        cursor.close();
        return activities;
    }
}
